(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

open XNum
open Logging
open ParseState
  
(* |begin_paragraph| starts a new paragraph, |end_paragraph| adds the current paragraph to the page. *)
let begin_paragraph ps =
  open_node_list ps `Paragraph
  
let end_paragraph ps =
  match close_node_list ps `Paragraph with
    | [] -> ()
    | nodes -> add_node ps (Node.Paragraph (location ps, nodes))
  
(* |begin_math| starts a math formula, |end_math| adds it to the current paragraph. *)
let begin_math ps = open_node_list ps `Math
  
let end_math ps =
  match close_node_list ps `Math with
    | [] -> ()
    | nodes -> add_node ps (Node.Math (location ps, nodes))
  
let begin_hbox ps = open_node_list ps `HBox
  
let end_hbox ps =
  match close_node_list ps `HBox with
    | [] -> ()
    | nodes -> add_node ps (Node.HBox (location ps, `Default, nodes))
  
let end_lrbox ps =
  match close_node_list ps `LRBox with
    | [] -> ()
    | nodes -> add_node ps (Node.HBox (location ps, `LR, nodes))
  
let end_rlbox ps =
  match close_node_list ps `RLBox with
    | [] -> ()
    | nodes -> add_node ps (Node.HBox (location ps, `RL, nodes))
  
(* |ensure_par_mode| enters paragraph mode if the system is in document mode. *)
let ensure_par_mode ps =
  match current_mode ps with
    | `Galley -> begin_paragraph ps
    | `VBox -> begin_hbox ps
    | `Paragraph | `Math | `HBox | `LRBox | `RLBox -> ()
    | m ->
        log_error (location ps)
          ("You can't start a paragraph in " ^
             ((ParseState.mode_to_string m) ^ " mode!"))
  
let leave_par_mode ps =
  match current_mode ps with
    | `Preamble | `Galley | `VBox -> ()
    | `Paragraph -> end_paragraph ps
    | `Math -> (end_math ps; end_paragraph ps)
    | `HBox -> end_hbox ps
    | `LRBox -> end_lrbox ps
    | `RLBox -> end_rlbox ps
    | m ->
        log_error (location ps)
          ("Mode " ^ ((ParseState.mode_to_string m) ^ " at end of paragraph!"))
  
(* |set_mode <mode>| sets the current mode and returns |True| on success. *)
let set_mode ps mode =
  let set_mode_par ps =
    match current_mode ps with
      | `Galley -> (begin_paragraph ps; true)
      | `Paragraph -> true
      | `Math -> (end_math ps; true)
      | _ -> false in
  let set_mode_math ps =
    match current_mode ps with
      | `Galley -> (begin_paragraph ps; begin_math ps; true)
      | `Paragraph -> (begin_math ps; true)
      | `Math -> true
      | `HBox | `LRBox | `RLBox -> (begin_math ps; true)
      | _ -> false
  in
    match mode with
      | `Galley -> (leave_par_mode ps; true)
      | `Paragraph -> set_mode_par ps
      | `Math -> set_mode_math ps
      | _ -> false
